package com.generalbackend.role.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generalbackend.common.util.BeanCopierUtils;
import com.generalbackend.common.util.ClientUtil;
import com.generalbackend.common.util.SnowFlakeIdGeneratorUtil;
import com.generalbackend.role.mapper.RoleMapper;
import com.generalbackend.role.model.dto.RoleDTO;
import com.generalbackend.role.model.entity.Role;
import com.generalbackend.role.model.entity.RolePermission;
import com.generalbackend.role.model.query.RoleQuery;
import com.generalbackend.role.service.IRolePermissionService;
import com.generalbackend.role.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static com.generalbackend.common.constants.Constants.DATA;
import static com.generalbackend.common.constants.Constants.TOTAL;

/**
* @Description 角色表业务层实现类
* @author tim.xie
* @date 2021-12-16 10:51
*/

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    private Logger LOG = LoggerFactory.getLogger(RoleServiceImpl.class);

    private static final Map<Long, String> roleMapCache = new ConcurrentHashMap<>();

	@Autowired
	private RoleMapper roleMapper;
	@Autowired
    private IRolePermissionService rolePermissionService;

	@Override
	public Boolean insert(RoleDTO roleDTO) {
	    roleDTO.setId(SnowFlakeIdGeneratorUtil.nextId());
	    roleDTO.setCreateUser(ClientUtil.getUser().getId());
        Integer num = roleMapper.insert(roleDTO);
        rolePermissionService.addRolePermission(roleDTO.getPermissionList(), roleDTO.getId());
        if(num <= 0) {
            return false;
         }
        roleMapCache.put(roleDTO.getId(), roleDTO.getName());
		return true;
	}

    @Override
    public Boolean insert(Role role) {
        role.setId(SnowFlakeIdGeneratorUtil.nextId());
        role.setCreateUser(ClientUtil.getUser().getId());
        Integer num = roleMapper.insert(role);
        if(num <= 0) {
            return false;
        }
        roleMapCache.put(role.getId(), role.getName());
        return true;
    }

    @Override
	public Boolean update(RoleDTO roleDTO) {
	    roleDTO.setUpdateUser(ClientUtil.getUser().getId());
	    roleDTO.setUpdateTime(new Date());
        Integer num = roleMapper.updateById(roleDTO);
        rolePermissionService.deleteByRole(roleDTO.getId());
        rolePermissionService.addRolePermission(roleDTO.getPermissionList(), roleDTO.getId());
        if(num <= 0) {
          return false;
        }
        roleMapCache.remove(roleDTO.getId());
        roleMapCache.put(roleDTO.getId(),roleDTO.getName());
		return true;
	}

	@Override
	public Boolean deleteById(Long id) {
        roleMapper.deleteById(id);
        rolePermissionService.deleteByRole(id);
		return true;
	}

	@Override
	public RoleDTO selectById(Long id) {
        Role role = roleMapper.selectById(id);
        RoleDTO roleDTO = BeanCopierUtils.copyOne2One(role, RoleDTO.class);
        List<RolePermission> permissions = rolePermissionService.getByRoleId(id);
        if (permissions !=null && !permissions.isEmpty()) {
            roleDTO.setPermissionList(permissions.stream()
                    .map(map -> map.getPermission()).collect(Collectors.toList()));
        }
        return roleDTO;
	}
    @Override
    public JSONObject list(RoleQuery roleQuery) {
        JSONObject result = new JSONObject();
        Page<Role> page = new Page<>(roleQuery.getPageNo(), roleQuery.getPageSize());
        List<Role> roleList = roleMapper.list(page,roleQuery);
        result.put(TOTAL,  page.getTotal());
        result.put(DATA, roleList);
        return result;
    }

    @Override
    public Boolean exit(Long roleId) {
        Role role = getById(roleId);
        return role == null ? false : true;
    }

    @Override
    public List<Role> getAllRole() {
        return roleMapper.selectList(null);
    }


}
